<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        body {
            height: 2000px;
        }
        
        #div1 {
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
        }
    </style>
</head>

<body height="2000px">
    <div id="div1"></div>

    <script>
        let div1 = document.querySelector("#div1");
        document.onmousemove = function(e) {
            e = e || window.e
            let st = document.documentElement.scrollTop || document.body.scrollTop;
            let sl = document.documentElement.scrollLeft || document.body.scrollLeft;
            // let x = e.pageX;
            // let y = e.pageY;   //鼠标相对于页面的距离  IE8以下不兼容
            let x = e.clientX;
            let y = e.clientY;
            div1.style.left = sl + x + "px";
            div1.style.top = +st + y + "px";
        }
    </script>
</body>

</html>